package com.example.recharge;

import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.ToggleButton;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class two0 extends AppCompatActivity implements View.OnClickListener {
    private Context mContext;
    private EditText et000;
    private CheckBox cb000;
    private CheckBox cb111;
    private Button bu000;
    private EditText et111;
    private ToggleButton tbPasswordVisibility;
    private Button bu111;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.two);
        //设置一个自动跳转页面的程序
//        new Handler().postDelayed(new Runnable() {
//            public void run() {
//                Intent it= new Intent();
//                it.setClass(two0.this, three0.class);
//                two0.this.startActivity(it);
//            }
//        }, 1000);
        mContext=this;
        //找到相应控件
        cb111=(CheckBox)findViewById(R.id.cb111);
        bu000=(Button) findViewById(R.id.bu000);
        cb000=(CheckBox) findViewById(R.id.cb000);
        bu111=(Button)findViewById(R.id.bu111);
        et000 = (EditText) findViewById(R.id.et000);
        et111=(EditText) findViewById(R.id.et111);
        //密码显示之获取组件
        this.tbPasswordVisibility=(ToggleButton)findViewById(R.id.tb_password_visibility);
        //事件注册
        this.tbPasswordVisibility.setOnCheckedChangeListener(new ToggleButtonClick());
//        //设置自动登录功能
//        boolean isrem=cb111.isChecked();
//        if (isrem){
//            //cb111.setChecked(true);
//            //设置一个自动跳转页面的程序
//        new Handler().postDelayed(new Runnable() {
//            public void run() {
//                Intent it= new Intent();
//                it.setClass(two0.this, three0.class);
//                two0.this.startActivity(it);
//                cb111.setChecked(true);
//            }
//        }, 1000);
//        }
        //回显用户名密码
        Map<String, String> map = UserInfoUtil.getUserInfo_android(mContext);
            if (map !=null){
            String username=map.get("username");
            String password=map.get("password");
            et000.setText(username);
            et111.setText(password);
            cb000.setChecked(true);//设置复选框选中状态
        }
        //设置按钮的点击事件
        bu000.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //在onclick方法中，获取用户输入的用户名和是否记住密码
                String username=et000.getText().toString().trim();
                String password=et111.getText().toString().trim();
                boolean isrem=cb000.isChecked();
                //判断用户密码是否为空，不为空请求服务器（省略默认请求成功）
                if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)){
                    Toast.makeText(mContext,"用户名密码不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                //判断是否记住密码，如果记住，将用户名保存到本地
                if (isrem){
                    boolean result= (boolean) UserInfoUtil.saveUserInfo_androdi(mContext,username,password);
                    if (result){
                        Toast.makeText(mContext,"用户密码保存成功",Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(two0.this,three0.class));
                    }
                }else{
                    Toast.makeText(mContext,"无需保存,登录成功",Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(two0.this,three0.class));
                }
            }
        });
        bu111.setOnClickListener(this);
        bu111.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(mContext,"待开发中...",Toast.LENGTH_SHORT).show();
            }
        });
    }
    @Override
    public void onClick(View view) {
    }
    private class ToggleButtonClick implements CompoundButton.OnCheckedChangeListener{
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
            //5、判断事件源的选中状态
            if (isChecked){
                //显示密码
                //etPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                et111.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            }else {
                // 隐藏密码
                //etPassword.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);
                et111.setTransformationMethod(PasswordTransformationMethod.getInstance());
            }
            //6、每次显示或者关闭时，密码显示编辑的线不统一在最后，下面是为了统一
            et111.setSelection(et111.length());
        }
    }
}
